package com.grantz.function.bbs.core.controller;

import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.grantz.function.bbs.core.entity.DiscussChild;
import com.grantz.function.bbs.core.inter.DiscussChildService;
import com.grantz.model.bbs.DiscussChildModel;
import com.grantz.model.bbs.DiscussChildSearch;
import com.grantz.models.base.element.page.ElementPage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/api/v1/discusschild")
@Api(value="子评论记录",tags={"子评论记录接口"})
public class DiscussChildController {

	@Resource
	private DiscussChildService discussChildService;
	
	// 获取群组用户
	@ApiOperation(value = "获取分页信息")
	@GetMapping
	public ResponseEntity<ElementPage<DiscussChildModel>>  get(DiscussChildSearch query) throws Exception{
		ElementPage<DiscussChildModel> element =  discussChildService.queryElementPage(query);
		return new ResponseEntity<ElementPage<DiscussChildModel>>(element ,HttpStatus.CREATED);
	}
	
	@ApiOperation(value = "保存,群组")
	@PutMapping
	public ResponseEntity<?>  save(@RequestBody DiscussChildModel model){
		discussChildService.save(pop(new DiscussChild() ,model));
		return new ResponseEntity<>(HttpStatus.CREATED);
	}

	@ApiOperation(value = "更新,群组")
	@PostMapping
	public ResponseEntity<?>  update(@RequestBody DiscussChildModel model){
		Optional<DiscussChild> option = discussChildService.findOneById(model.getId());
		if(option.isPresent()) {
			discussChildService.save(pop(option.get() , model));
		}
		return new ResponseEntity<>(HttpStatus.CREATED);
	}

	private DiscussChild pop(DiscussChild entity ,DiscussChildModel model) {
//		entity.setGroupName(model.getGroupName());
//		entity.setGroupDesc(model.getGroupDesc());
//		entity.setLongitude(model.getLongitude());
//		entity.setLatitude(model.getLatitude());
//		entity.setBigType(model.getBigType());
//		entity.setMidType(model.getMidType());
		return entity;
	}
}
